/*
PlayerShutdown v2.1
(c) mc_kibel // http://se-developers.net / http://centrumSE.pl/forum
Elf allows to shutdown AudioPlayer after set time
Time can be set in BCFG file (in minutes) - default 20min
You can set language in BCFG file (Polish / English) - default English
You can set key to activate elf
Activating: Run elf, turn on music, go to "Now Playing", press "Activate Key",
Elf will create a GUI - instruction is not needed ;-) 
After "Start snooze" enter snooze time and press ok. Player will be closed after set time.

Changelog:
-Added GUI to elf 
-"Start snooze" will close the player after time set in BCFG
-"Configure Elf" will open BCFG file in BcfgEdit.elf
="Close Elf" will "kill" the program
-KEYPLAYSTOP_PRESSED_EVENT  no more needed! It will work w/out problems on W810i

*/


#include "..\\include\Lib_Clara.h"
#include "..\\include\Dir.h"
#include "..\\include\cfg_items.h"
#include "conf_loader.h"
#include "config_data.h"
#define ELF_BCFG_CONFIG_EVENT 994

typedef struct
{
BOOK book;
GUI_LIST*gui;
}MyBOOK;

typedef struct
{
  char dummy1[0x1C];
  void *gui;
  char dummy2[0x54];
}MediaPlayerBook;

u16 timer;
MyBOOK * myBook;

unsigned int Working = 0;
int SLEEP;

int isRunned(BOOK * book)
{
  if(!strcmp(book->xbook->name,"PlayerShutdown")) 
    return 1;
  
  return 0;
}

void onTimer (u16 unk , void * data)
{
  {
    BOOK * Audio = FindBook(isAudioPlayerBook());
    if (Audio) 
      BookObj_KillBook(Audio);
    
    Working = 0;
    MessageBox(0x6fFFFFFF, STR("Music turned off") ,0, 1 ,5000,0); 
    Timer_Kill(&timer);
  }
}


int TerminateElf(void * ,BOOK* book)
{
  FreeBook(book);
  return(1);
}

typedef struct
{
  BOOK * book;
}MSG;

int ShowAuthorInfo(void *mess ,BOOK* book)
{
  MSG * msg = (MSG*)mess;
  MessageBox(0x6fFFFFFF,STR("PlayerShutdown\n(c) mc_kibel\nThanks: Zylka, UltraShot\nCentrumSE.pl\nSE-Developers.net"),0, 1 ,5000,msg->book);
  return(1);
}

static int ReconfigElf(void *mess ,BOOK *book)
{
  RECONFIG_EVENT_DATA *reconf=(RECONFIG_EVENT_DATA *)mess;
  int result=0;
  if (wstrcmpi(reconf->path,successed_config_path)==0 && wstrcmpi(reconf->name,successed_config_name)==0)
  {
    InitConfig();
    result=1;
  }
  return(result);
}

int onBcfgConfig(void* mess,BOOK* b)
{
  FSTAT _fstat;
  wchar_t path[256];
  
  if(fstat(GetDir(DIR_ELFS|MEM_INTERNAL),L"BcfgEdit.elf",&_fstat)==0)
    wstrcpy(path,GetDir(DIR_ELFS|MEM_INTERNAL));
  else if(fstat(GetDir(DIR_ELFS|MEM_EXTERNAL),L"BcfgEdit.elf",&_fstat)==0)
    wstrcpy(path,GetDir(DIR_ELFS|MEM_EXTERNAL));
  else
  {
    MessageBox(0x6FFFFFFF, STR("BCFGEdit not found in ZBin!") , 0, 1 ,5000, 0);
    return (1);
  }
  wstrcat(path,L"/BcfgEdit.elf");
  elfload(path,(void*)successed_config_path,(void*)successed_config_name,0);
  return (1);
}

void GuiBack(BOOK *bk, void *)
{
  BookObj_Hide(FindBook(isRunned),0);
  GUI_Free(myBook->gui);
}

void GuiOnEnterPressed(BOOK *bk, void *lt)
{     
  int item=ListMenu_GetSelectedItem(myBook->gui);
  if(item==0)
  {
    BOOK * aud = FindBook(isAudioPlayerBook());
    if (aud)
    {
      if (Working==0)
      {
        SLEEP = Time_Sleep * 60000;
        timer=Timer_Set(SLEEP,onTimer,0);
        MessageBox(0x6fFFFFFF,STR("Player will be closed after set time"),0, 1 ,5000,0);
        Working=1;
      }
      else
        MessageBox(0x6fFFFFFF, STR("Timer is already working") ,0, 1 ,5000,0);
    }
  }
  else if(item==1)
  {
    FSTAT _fstat;
    wchar_t path[256];
    if(fstat(GetDir(DIR_ELFS|MEM_INTERNAL),L"BcfgEdit.elf",&_fstat)==0)
      wstrcpy(path,GetDir(DIR_ELFS|MEM_INTERNAL));
    
    else if(fstat(GetDir(DIR_ELFS|MEM_EXTERNAL),L"BcfgEdit.elf",&_fstat)==0)
      wstrcpy(path,GetDir(DIR_ELFS|MEM_EXTERNAL));
    
    else
      MessageBox(0x6FFFFFFF, STR("BCFGEdit not found in ZBin!"), 0, 1 ,5000, 0);
    
    wstrcat(path,L"/BcfgEdit.elf");
    elfload(path,(void*)successed_config_path,(void*)successed_config_name,0);
  }
  else if (item==2)
  {
    FreeBook(FindBook(isRunned));
    MessageBox(0x6FFFFFFF, STR("Closed") , 0, 1 ,5000, 0);
  }
}

int GuiOnLBMessage(GUI_MESSAGE * msg)
{
  int item;  
  switch(msg->msg)
  {    
  case 1:
    item=GUIonMessage_GetCreatedItemIndex(msg);    
    if(item==0)
      SetMenuItemText0(msg,STR("Start sleep"));
    else if(item==1)
      SetMenuItemText0(msg,STR("Configure elf")); 
    else if(item==2)
      SetMenuItemText0(msg,STR("Close elf"));
    break;
  }
  return(1);
};

void GuiCreateGuiList(void * r0, BOOK * bk, wchar_t * GUI_NAME, int ItemNumber)
{  
  myBook->gui=CreateListObject(bk,0);
  GuiObject_SetTitleText(myBook->gui,Str2ID(GUI_NAME,0,SID_ANY_LEN));
  SetNumOfMenuItem(myBook->gui,ItemNumber);
  OneOfMany_SetonMessage((GUI_ONEOFMANY*)myBook->gui,GuiOnLBMessage);
  SetCursorToItem(myBook->gui,0);
  SetMenuItemStyle(myBook->gui,0);
  GUIObject_Softkey_SetAction(myBook->gui,ACTION_BACK, GuiBack);  
  GUIObject_Softkey_SetAction(myBook->gui,ACTION_LONG_BACK, GuiBack);  
  GUIObject_Softkey_SetAction(myBook->gui,ACTION_SELECT1,GuiOnEnterPressed);     
  ShowWindow(myBook->gui);  
};

void RunGUI (BOOK*b,void*)
{
      GuiCreateGuiList(0,(BOOK*)myBook,L"PlayerShutdown",3);
      BookObj_SetFocus(FindBook(isRunned),0);
}


int NewKey (int key, int r1, int mode)
{
    BOOK * bk = DISPLAY_GetTopBook(0);
    if (!strcmp(bk->xbook->name,"AudioPlayerBook"))
    {
      void *mpgui;
      mpgui=((MediaPlayerBook*)FindBook(isAudioPlayerBook()))->gui;
      MediaPlayer_SoftKey_SetText(mpgui,51,STR("Sleep"));
      MediaPlayer_SoftKey_SetAction(mpgui,51,RunGUI);
      MediaPlayer_SoftKey_SetItemAsSubItem(mpgui,20,51); // 21 - Softkey ID (2020)
      MediaPlayer_SoftKey_AddHelpStr(mpgui,51,STR("Turn off the music after set time."));
    }  
  return 0;
}


const PAGE_MSG HW_PageEvents[]@ "DYN_PAGE" ={
  ELF_TERMINATE_EVENT , TerminateElf,
  ELF_SHOW_INFO_EVENT  , ShowAuthorInfo,
  ELF_RECONFIG_EVENT,    ReconfigElf,
  ELF_BCFG_CONFIG_EVENT,   onBcfgConfig,
  NIL_EVENT_TAG,           NULL
};

PAGE_DESC base_page ={"HW_BasePage",0,HW_PageEvents};


void elf_exit(void)
{
  kill_data(&ELF_BEGIN, (void(*)(void*))mfree_adr());
}

void onCloseHWBook(BOOK * book)
{
  if (book)
  {
    Timer_Kill(&timer);
    ModifyKeyHook(NewKey,0);
    SUBPROC(elf_exit);
  }
}

MyBOOK * CreateHWBook()
{
  myBook= new MyBOOK;  
  CreateBook(myBook,onCloseHWBook,&base_page,"PlayerShutdown",-1,0);
  return(myBook);
}


int main (void)
{
  InitConfig();
  if (FindBook(isRunned))
    MessageBox(0x6fFFFFFF, STR("PlayerShutdown is already working!") ,0, 1 ,5000,0); 
  else 
  {
    CreateHWBook();
    ModifyKeyHook(NewKey,1);
  }
  return(0);
}
